﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Events;

namespace HYView
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdView3d : YJKCommandManual
	{
		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			CmdViewType types = (CmdViewType)127;
			if (SwitchView.SetUsableView(types, cmdData) != Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			try
			{
				Document document = cmdData.Application.ActiveUIDocument.Document;
				cmdData.Application.ViewActivating += this.Application_ViewActivating;
				View3D view3D = CmdView3d.GetView(document);
				if (view3D == null)
				{
					Transaction transaction = new Transaction(document, "cv");
					transaction.Start();
					try
					{
                        ViewFamilyType viewFamilyType = new FilteredElementCollector(document).OfClass(typeof(ViewFamilyType)).Cast<ViewFamilyType>().FirstOrDefault((ViewFamilyType x) => 102 == (int)x.ViewFamily);
						view3D = View3D.CreateIsometric(document, viewFamilyType.Id);
						view3D.Name = "盈建科三维模型浏览";
						view3D.DetailLevel = cmdData.Application.ActiveUIDocument.ActiveView.DetailLevel;
						transaction.Commit();
					}
					catch
					{
						transaction.RollBack();
					}
				}
				cmdData.Application.ActiveUIDocument.ActiveView = view3D;
				try
				{
					ShowDlgApp.ThisApp.ShowForm(cmdData.Application);
					cmdData.Application.Application.DocumentChanged += this.Application_DocumentChanged;
				}
				catch (Exception ex)
				{
					MessageBox.Show(ex.Message, "盈建科提示");
				}
			}
			catch (Exception ex2)
			{
				MessageBox.Show(ex2.Message, "盈建科提示");
			}
			return base.YJKExcute(cmdData, ref message, elements);
		}

		private void Application_DocumentChanged(object sender, DocumentChangedEventArgs e)
		{
			if (e.GetDeletedElementIds().Count > 0)
			{
				IEnumerator<ElementId> enumerator = e.GetDeletedElementIds().GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						ElementId id = enumerator.Current;
						foreach (object obj in ShowDlgApp.ThisApp.m_MyForm.tvByCategory.Nodes)
						{
							TreeNode tn = (TreeNode)obj;
							this.delElement(tn, id);
						}
						foreach (object obj2 in ShowDlgApp.ThisApp.m_MyForm.tvByLevel.Nodes)
						{
							TreeNode tn2 = (TreeNode)obj2;
							this.delElement(tn2, id);
						}
					}
					return;
				}
			}
			if (e.GetModifiedElementIds().Count > 0)
			{
				foreach (ElementId elementId in e.GetModifiedElementIds())
				{
                    Autodesk.Revit.DB.View view = e.GetDocument().GetElement(elementId) as Autodesk.Revit.DB.View;
				}
			}
		}

		private void delElement(TreeNode tn, ElementId id)
		{
			if (tn != null)
			{
				if (tn.Tag != null && tn.Tag is Element)
				{
					Element element = (Element)tn.Tag;
					try
					{
						if (element.Id == id)
						{
							tn.Remove();
						}
					}
					catch
					{
						tn.Remove();
					}
				}
				foreach (object obj in tn.Nodes)
				{
					TreeNode tn2 = (TreeNode)obj;
					this.delElement(tn2, id);
				}
			}
		}

		private void setElementVisable(TreeNode tn, ElementId id, bool vis)
		{
			if (tn != null)
			{
				if (tn.Tag != null && tn.Tag is Element)
				{
					Element element = (Element)tn.Tag;
					try
					{
						if (element.Id == id)
						{
							tn.Checked = vis;
						}
					}
					catch
					{
						tn.Checked = vis;
					}
				}
				foreach (object obj in tn.Nodes)
				{
					TreeNode tn2 = (TreeNode)obj;
					this.setElementVisable(tn2, id, vis);
				}
			}
		}

		private void Application_ViewActivating(object sender, ViewActivatingEventArgs e)
		{
			if (e.NewActiveView.Name != "盈建科三维模型浏览")
			{
				ShowDlgApp.ThisApp.CloseForm();
			}
		}

		private static View3D GetView(Document doc)
		{
			View3D result = null;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(typeof(View3D));
			foreach (Element element in filteredElementCollector.ToElements())
			{
				if (element.Name == "盈建科三维模型浏览")
				{
					result = (element as View3D);
					break;
				}
			}
			return result;
		}
	}
}
